package com.xuelang.pipeline.artifact.app.nexus3v1.xo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * @author xiwen.zxg
 * @since 2024/7/10
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ApiResponseXO<T> {

    private int status;

    private String id;

    private String message;

    private T data;

    public boolean isSuccess() {
        return HttpStatus.valueOf(status).is2xxSuccessful();
    }

    public boolean isError() {
        return !isSuccess();
    }

    public static <T> ApiResponseXO<T> success() {
        return ApiResponseXO.<T>builder().id("*").message("SUCCESS").status(HttpStatus.OK.value()).data(null).build();
    }

    public static <T> ApiResponseXO<T> success(T data) {
        return ApiResponseXO.<T>builder().id("*").message("SUCCESS").status(HttpStatus.OK.value()).data(data).build();
    }

    public static <T> ApiResponseXO<T> error(int status) {
        return ApiResponseXO.<T>builder().status(status).message("FAILURE").id("*").build();
    }

    public static <T> ApiResponseXO<T> error(int status, String message) {
        return ApiResponseXO.<T>builder().status(status).message(message).id("*").build();
    }

    public static <T> ApiResponseXO<T> error(int status, String message, String id) {
        return ApiResponseXO.<T>builder().status(status).message(message).id(id).build();
    }
}
